package com.example.Lezedata.ui.activity.goods;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.example.Lezedata.R;
import com.example.Lezedata.dataModel.ProgressModel;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

public class ProgressView extends FrameLayout {
    private View progressBar;
    private TextView percentageTextView;
    private TextView numbersTextView;
    private ProgressModel model;

    public ProgressView(@NonNull Context context) {
        super(context);
        initView();
    }

    public ProgressView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.progress_view, this);
        initView();
    }

    void initView(){
        progressBar = findViewById(R.id.progressBarId);
        percentageTextView = findViewById(R.id.percentageTextViewId);
        numbersTextView = findViewById(R.id.numbersTextViewId);
    }

    public void setModel(ProgressModel model) {
        this.model = model;
        float proportion = (float)model.current / model.total;

        ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) progressBar.getLayoutParams();
        params.matchConstraintPercentWidth = proportion;
        progressBar.setLayoutParams(params);

        String pro = String.valueOf(model.current * 100/model.total);
        percentageTextView.setText(pro + "%");

        String s = String.valueOf(model.current);
        String str = s + "/" + model.total;
        SpannableString spannableString = new SpannableString(str);
        ForegroundColorSpan textColor = new ForegroundColorSpan(Color.parseColor("#F3B72C"));
        spannableString.setSpan(textColor, 0, s.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        numbersTextView.setText(spannableString);
    }
}
